using System.Reflection;

namespace ShadeTree.Binding
{
    public class ChildScreenBinder<T> : IBoundPart where T : class
    {
        private readonly IScreenBinder _innerBinder;
        private readonly PropertyInfo _property;

        public ChildScreenBinder(PropertyInfo property, IValidationMonitor monitor)
        {
            _property = property;
            _innerBinder = new ScreenBinder<T>(monitor);
        }


        public ChildScreenBinder(IScreenBinder innerBinder, PropertyInfo property)
        {
            _innerBinder = innerBinder;
            _property = property;
        }

        public IScreenBinder InnerBinder
        {
            get { return _innerBinder; }
        }

        #region IBoundPart Members

        public void Bind(object model)
        {
            object target = _property.GetValue(model, null);
            _innerBinder.BindToModel(target);
        }

        public bool ApplyChanges()
        {
            return _innerBinder.ApplyChangesToModel();
        }

        public void Reset()
        {
            _innerBinder.ResetToOriginalValues();
        }

        public void Update()
        {
            _innerBinder.UpdateScreen();
        }

        public IScreenBinder Binder
        {
            set
            {
                // no-op 
            }
        }

        public string FieldName
        {
            get { return _property.Name; }
        }

        public void StopBinding()
        {
        }

        public void SetDefaults()
        {
            _innerBinder.SetDefaultValues();
        }

        #endregion
    }
}